//
//  SearchView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/29.
//

#import "SearchView.h"

@interface SearchView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *qrBtn;
@property (nonatomic, strong) UITextField *searchTF;

@end

@implementation SearchView

- (void)initDidLoad {
    self.backgroundColor = [UIColor whiteColor];
}
- (void)initViews {
    [self addSubview:self.qrBtn];
    UIView *bgview = [[UIView alloc] init];
    bgview.backgroundColor = kRGB(0xF7F7F7);
    bgview.layer.cornerRadius = 18;
    [self addSubview:bgview];
    
    UIImageView *searchImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_search"]];
    [bgview addSubview:searchImageV];
    [bgview addSubview:self.searchTF];
    
    [self.qrBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(20);
        make.leading.mas_equalTo(16);
        make.top.mas_equalTo(18);
    }];
    [bgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.qrBtn.mas_trailing).offset(12);
        make.centerY.equalTo(self.qrBtn);
        make.trailing.mas_equalTo(-16);
        make.height.mas_equalTo(36);
    }];
    [searchImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(14);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(14);
    }];
    [self.searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(searchImageV.mas_trailing).offset(5);
        make.trailing.mas_equalTo(-14);
        make.top.bottom.mas_equalTo(0);
    }];
}

- (void)setType:(int)type {
    _type = type;
    self.searchTF.placeholder = type == 1 ? @"请输入商家昵称" : @"请输入商品标题";
}

- (UIButton *)qrBtn {
    if (!_qrBtn) {
        _qrBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_qrBtn setImage:[UIImage imageNamed:@"icon_qr"] forState:UIControlStateNormal];
        @weakify(self)
        [[_qrBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.qrBlock) {
                self.qrBlock();
            }
        }];
    }
    return _qrBtn;
}
- (UITextField *)searchTF {
    if (!_searchTF) {
        _searchTF = [[UITextField alloc] initWithFrame:CGRectZero];
        _searchTF.placeholder = @"请输入商品标题";
        _searchTF.textColor = kRGB(0x333333);
        _searchTF.font = kFontRegula(12);
        _searchTF.returnKeyType = UIReturnKeySearch;
        _searchTF.delegate = self;
        @weakify(self)
        [_searchTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
         @strongify(self)
            if (self.searchTF.text.length == 0) {
                if (self.clearBlcok) {
                    self.clearBlcok();
                }
            }
        }];
    }
    return _searchTF;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.text.length <= 0) {
        kShowText(self.type == 1 ? @"请输入商家昵称" : @"请输入商品标题");
        return NO;
    }
    [self.searchTF resignFirstResponder];
    if (self.searchBlock) {
        self.searchBlock(textField.text);
    }
    return YES;
}

@end
